package com.example.tourstor;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyPlanActivity2 extends AppCompatActivity {

    private List<HashMap<String,String>> listItems;
    private HashMap<String,String> map;
    private SimpleAdapter listItermAdapter;
    ListView listView;

    private String tstart,tdestin,tstart1;
    private String TAG="MyPlanActivity2";

    private TextView Tstart,title,Tdestin;
    private String name;

    private Button title_btn_left,title_btn_right;
    DBOpenHelper dbOpenHelper;
    Connection con ;
    private PreparedStatement ps=null;
    private ResultSet rs=null;
    Thread t1;
    Handler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_my_plan2);
        setTitle();


        init();
        list();


    }
    public void init(){
        Tstart=findViewById(R.id.Tstart2);
        Tdestin=findViewById(R.id.Tdestin2);

        listView = findViewById(R.id.plans);

        listView.setEmptyView(Tstart);
        listView.setEmptyView(Tdestin);

    }

    
    public void list() {

        final SharedPreferences sharedPreferences=getSharedPreferences("Cname", Activity.MODE_PRIVATE);
        //name=sharedPreferences.getString("Cname",null);
        name="41623060";
        Log.i(TAG, "MyPlanActivity2: 登录名是："+name);

        if(name!=null){
            t1=new Thread(){
                public void run(){
                    con=dbOpenHelper.getConn();
                    String sql="select * from makeplan where Uid= '"+name+"'";
                    listItems = new ArrayList<HashMap<String, String>>();
                    try {
                        ps= (PreparedStatement) con.prepareStatement(sql);
                        if(ps!=null){
                            rs= ps.executeQuery();
                            if(rs!=null){
                                while(rs.next()){
                                    map = new HashMap<String, String>();
                                    tstart=rs.getString("Tstart");
                                    tdestin=rs.getString("Tdestin");
                                    map.put("Tstart",  tstart);
                                    map.put("Tdestin",  tdestin);
                                    listItems.add(map);
                                    Log.i(TAG, "thread: Tstart="+tstart+" and Tdestin="+tdestin);
                                }
                                Message msg = handler.obtainMessage(7);
                                msg.obj =listItems ;
                                handler.sendMessage(msg);
                            }
                        }
                        con.close();//一定要关闭
                        ps.close();
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            };
            t1.start();
            handler=new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    if(msg.what==7){
                        listItermAdapter=new SimpleAdapter(MyPlanActivity2.this,listItems,R.layout.list_item,
                                new String[]{"Tstart","Tdestin"},
                                new int[]{R.id.Tstart2,R.id.Tdestin2});
                        listView.setAdapter(listItermAdapter);
                        Log.i(TAG, "handleMessage: 显示我的计划");
                        listItermAdapter.notifyDataSetChanged();

                    }
                    super.handleMessage(msg);
                }
            };

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    HashMap<String,String> map1= (HashMap<String, String>) parent.getItemAtPosition(position);
                    tstart1=map1.get("Tstart");
                    Log.i(TAG, "onItemLongClick: "+tstart1);

                    Intent intent=new Intent(MyPlanActivity2.this,MyPlanActivity3.class);
                    intent.putExtra("Tstart",tstart1);
                    startActivity(intent);

                }
            });


        }else {
            Toast.makeText(MyPlanActivity2.this,"登陆后才能查看计划",Toast.LENGTH_LONG).show();
        }

    }


    public void setTitle(){
        title = findViewById(R.id.title_text);
        title.setText("我的计划");
        title_btn_left = findViewById(R.id.title_btn_left);
        title_btn_right=findViewById(R.id.title_btn_right);
        title_btn_left.setText("");
        title_btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
